﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for SqlDataHelper
/// </summary>
public class SqlDataHelper : SqlDataProvider
{
    //Fields.
    private String sql;

    //Properties.
    protected String Sql
    {
        set { this.sql = value; }
        get { return this.sql; }
    }
	public SqlDataHelper()
	{
		//
		// TODO: Add constructor logic here
		//
	}


    protected DataSet ExecuteQuery(String sql)
    {
        DataSet ds;
        SqlDataAdapter da;

        try
        {
            this.MoKetNoi();
            da = new SqlDataAdapter(sql, this.SQLConnection);
            ds = new DataSet();
            da.Fill(ds);
            this.DongKetNoi();

            return ds;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    //Thực thi NonQuery (Hàm dùng để Thêm, xóa, sửa). Trả về true: Thành công, false: Ko thành công.
    protected bool ExecuteNonQuery(String sql)
    {
        bool check = false;
        SqlCommand cmd;

        try
        {
            this.MoKetNoi();
            cmd = new SqlCommand(sql, this.SQLConnection);
            check = (cmd.ExecuteNonQuery()) > 0 ? true : false;
            this.DongKetNoi();
            return check; //check = true: Thành công, check = false: Ko thành công.
        }
        catch
        {
            return check;
        }
    }
}
